package com.ce.common.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.util.TypedValue;

import java.lang.reflect.Method;

public class DisplayUtils
{
	/**
	 * 将px值转换为dip或dp值，保证尺寸大小不变
	 * 
	 * @param pxValue
	 * @param pxValue
	 *            （DisplayMetrics类中属性density）
	 * @return
	 */
	public static int px2dip(Context context, float pxValue)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public static int getTextHeight(Paint paint) {
		Paint.FontMetrics fontMetrics = paint.getFontMetrics();
		return (int) (fontMetrics.bottom - fontMetrics.top + 0.5f);
	}

	public static int getPxFromResId(Context context, int resId) {
		return context.getResources().getDimensionPixelSize(resId);
	}


	public static int dpToPx(float dp, Resources resources) {
		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
		return (int) px;
	}

	/**
	 * 将dip或dp值转换为px值，保证尺寸大小不变
	 * 
	 * @param dipValue
	 * @param dipValue
	 *            （DisplayMetrics类中属性density）
	 * @return
	 */

	public static int dip2px(Context context,float dipValue)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	/**
	 * 将px值转换为sp值，保证文字大小不变
	 * 
	 * @param pxValue
	 * @param pxValue
	 *            （DisplayMetrics类中属性scaledDensity）
	 * @return
	 */
	public static float px2sp(Context context, float pxValue)
	{
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return pxValue / fontScale;
	}

	/**
	 * 将sp值转换为px值，保证文字大小不变
	 * 
	 * @param spValue
	 * @param spValue
	 *            （DisplayMetrics类中属性scaledDensity）
	 * @return
	 */
	public static float sp2px(Context context, float spValue)
	{
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return spValue * fontScale;
	}

	/**
	 * 判断是否有虚拟按键
	 * @param context
	 * @return
	 */
	public static boolean checkDeviceHasNavigationBar(Context context)
	{
		boolean hasNavigationBar = false;
		Resources rs = context.getResources();
		int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
		if (id > 0)
		{
			hasNavigationBar = rs.getBoolean(id);
		}
		try
		{
			Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
			Method m = systemPropertiesClass.getMethod("get", String.class);
			String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
			if ("1".equals(navBarOverride))
			{
				hasNavigationBar = false;
			}
			else if ("0".equals(navBarOverride))
			{
				hasNavigationBar = true;
			}
		}
		catch (Exception e)
		{
		}
		return hasNavigationBar;
	}

	public static int getDisplayWidth(Context context)
	{
		return context.getResources().getDisplayMetrics().widthPixels;
	}



	public static int getNavigationBarHeight(Context context)
	{
		Resources resources = context.getResources();
		int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
		int height = resources.getDimensionPixelSize(resourceId);
		return height;
	}


	public static int getFastInputHeight(int curKeyboardHeight)
	{
		int fastKeyH = (int) (curKeyboardHeight / 4 * 0.8f + 0.5f);
		return fastKeyH;
	}
}